#include <pluginmanager.h>
#include <systeminfoplugininterface.h>
#include <QDebug>
#include <QDir>
#include <QDirIterator>

PluginManager::PluginManager(QObject *parent) : QObject(parent) {}

void PluginManager::loadPluginsFromFolder(const QString &pluginPath)
{
    QDir dir(pluginPath);
    QDirIterator dit(dir.path(), QDir::Files);

    while (dit.hasNext())
    {
        QString fileName = dit.next();
        if (fileName.endsWith(".so"))
        {
            QPluginLoader loader(dir.absoluteFilePath(fileName));
            QObject *plugin = loader.instance();
            if (plugin)
            {
                SystemInfoPluginInterface *infoPlugin = qobject_cast<SystemInfoPluginInterface*>(plugin);
                if (infoPlugin)
                {
                    plugins.append(infoPlugin);
                    qDebug() << "Loaded plugin:" << infoPlugin->name();
                }
                else
                {
                    qDebug() << "Plugin is not a SystemInfoPluginInterface:" << fileName;
                }
            }
            else
            {
                qDebug() << "Failed to load plugin:" << fileName << loader.errorString();
            }
        }
    }


}

QList<SystemInfoPluginInterface*> PluginManager::getPlugins() const
{
    return plugins;
}

